package io.renren.restful.organization;


import com.baseframe.sys.entity.*;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController

@RequestMapping("/rest/organization")
public class OrganizationAPI {
    private static final String ALLOW_TYPE_DEPT = "Dept";
    private static final String ALLOW_TYPE_USER = "User";
    private static final String ALLOW_TYPE_ROLE = "Role";

    @Autowired
    private OrganizationApiService organizationApiService;

    @RequestMapping(value = "/getOu", method = RequestMethod.POST)
    public String getOu(@RequestBody String param) {
        RequestParam requestParam = getJsonObject(param);
        List<FrameOu> rcDeptList =organizationApiService.findOuListByType(requestParam,ALLOW_TYPE_DEPT);
        return responseJsonString(rcDeptList,"1","部门数据");
    }

    @RequestMapping(value = "/getUser", method = RequestMethod.POST)
    public String getUser(@RequestBody String param) {
        RequestParam requestParam = getJsonObject(param);
        List<FrameUser> rcDeptList =organizationApiService.findUserListByType(requestParam,ALLOW_TYPE_USER);
        return responseJsonString(rcDeptList,"1","用户数据");
    }

    @RequestMapping(value = "/getRole", method = RequestMethod.POST)
    public String getRole(@RequestBody String param) {
        RequestParam requestParam = getJsonObject(param);
        List<FrameRole> rcRoleList =organizationApiService.findRoleListByType(requestParam,ALLOW_TYPE_ROLE);
        return responseJsonString(rcRoleList,"1","角色数据");
    }

    @RequestMapping(value = "/getUserRole", method = RequestMethod.POST)
    public String getUserRole(@RequestBody String param) {
        RequestParam requestParam = getJsonObject(param);
        List<FrameUserRole> rcRoleList =organizationApiService.findUserRoleListByType(requestParam,ALLOW_TYPE_ROLE);
        return responseJsonString(rcRoleList,"1","角色数据");
    }

    /**
     * 将json body 转换为请求参数对象
     *
     * @param param
     * @return
     */
    private RequestParam getJsonObject(String param) {
        RequestParam requestParam = null;
        try {
            JsonParser jsonParser = new JsonParser();
            JsonObject jsonObject = jsonParser.parse(param).getAsJsonObject();
            String appKey = jsonObject.get("app_key").getAsString();
            String appSecret = jsonObject.get("app_secret").getAsString();
            String updateTime = jsonObject.get("update_time").getAsString();
            requestParam = new RequestParam(appKey, appSecret, updateTime);
        } catch (Exception e) {
            requestParam = new RequestParam("", "", "");
            e.printStackTrace();
        } finally {
            return requestParam;
        }
    }

    private String responseJsonString(Object obj,String status,String description){
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("status",status);
        jsonObject.addProperty("description",description);

        JsonArray jsonArray = new JsonParser().parse(new Gson().toJson(obj)).getAsJsonArray();
        jsonObject.add("result",jsonArray);
        return jsonObject.toString();
    }
}
